﻿using System;
using System.Collections.Generic;
using DMS.Business.Modules.Configuation;
using DMS.Business.Utility;

namespace DMS.Business.Modules.Organization
{
    /// <summary>
    /// 部门（组织单元）
    /// </summary>
    public class Department : Actor
    {
        #region 数据成员/内部成员
        protected string _FullName = string.Empty;
        protected string _IDPath = string.Empty;
        protected bool _IsTemporary = false;
        protected int _Level = -1;
        protected int _Sequence = int.MaxValue;

        protected string _ParentID = "";
        protected Department _Parent = null;
        protected Department _CountUnit = null;
        protected List<User> _Managers = null;
        protected bool _isHault = false;//是否服务厅
        
        #endregion

        #region 公共属性
        public override ActorTypes ActorType
        {
            get
            {
                return ActorTypes.Dept;
            }
            set
            {
            }
        }
        /// <summary>
        /// 部门全称
        /// </summary>
        public string FullName
        {
            get
            {
                return this._FullName;
            }
            internal set
            {
                this._FullName = value;
            }
        }

        /// <summary>
        /// 有意义的部门全称，不显示广州移动本部
        /// </summary>
        public string FullNameSenseful
        {
            get
            {
                return FullName.Replace("广州移动本部>", "");
            }
        }

        /// <summary>
        /// ID路径
        /// </summary>
        public string IDPath
        {
            get
            {
                return this._IDPath;
            }
            internal set
            {
                this._IDPath = value;
            }
        }
        /// <summary>
        /// 部门所在层次
        /// </summary>
        public int Level
        {
            get
            {
                return this._Level;
            }
            internal set
            {
                this._Level = value;
            }
        }
        /// <summary>
        /// 当前层次下的显示排位
        /// </summary>
        public int Sequence
        {
            get
            {
                return this._Sequence;
            }
            internal set
            {
                this._Sequence = value;
            }
        }
        /// <summary>
        /// 是否临时部门
        /// </summary>
        public bool IsTemporary
        {
            get
            {
                return this._IsTemporary;
            }
            set
            {
                this._IsTemporary = value;
            }
        }

        /// <summary>
        /// 上级部门ID
        /// </summary>
        public string ParentID
        {
            get
            {
                return _ParentID;
            }
            set
            {
                _ParentID = value;
            }
        }

        /// <summary>
        /// 上级部门
        /// </summary>
        public Department Parent
        {
            get
            {
                // 上级部门对象延迟加载
                if (this._Parent == null && this._ParentID != "")
                {
                    this._Parent = UserHelper.GetDepartmentByID(this._ParentID);
                }
                return this._Parent;
            }
            set
            {
                if (value != null)
                {
                    this._Parent = value;
                    this._ParentID = value.ID;
                }
                else
                {
                    this._Parent = value;
                    this._ParentID = "";
                }
            }
        }
        /// <summary>
        /// 下级部门
        /// </summary>
        public List<Department> SubDepartments
        {
            get
            {
                return UserHelper.GetSubDepartments(this);
            }
        }

        /// <summary>
        /// 是否拥有子部门
        /// </summary>
        public bool HasSubDeptments
        {
            get
            {
                return UserHelper.HasSubDeptments(this.ID);
            }
        }

        /// <summary>
        /// 室经理
        /// </summary>
        public List<User> Managers
        {
            get
            {
                if (this._Managers == null)
                {
                    this._Managers = this.GetManagers();
                }

                return this._Managers;
            }
        }
        /// <summary>
        /// 部门金额限制（-1为不限制）
        /// </summary>
        public double FundsQuota
        {
            get
            {
                MemberConfig config = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_DeptFundsQuota);
                if (config != null)
                {
                    return Convert.ToDouble(config.Value);
                }
                else
                {
                    SysConfig defaultConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_DeptFundsQuota);
                    return Convert.ToDouble(defaultConfig.Value);
                }
            }
        }
        /// <summary>
        /// 是否有部门金额限制
        /// </summary>
        public bool IsHasQuota
        {
            get
            {
                return this.FundsQuota >= 0;
            }
        }

        /// <summary>
        /// 是否汇总单位
        /// </summary>
        public bool IsCountUnit
        {
            get
            {
                bool res = false;
                MemberConfig config = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_IsDeptCountUnit);
                if (config != null)
                {
                    res = (config.Value == "1");
                }
                else
                {
                    SysConfig defaultConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_IsDeptCountUnit);
                    res = (defaultConfig.Value == "1");
                }

                return res;
            }
        }

        /// <summary>
        /// 是否服务厅
        /// </summary>
        public bool IsHault
        {
            get
            {
                bool res = false;
                MemberConfig config = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_IsHault);
                if (config != null)
                {
                    res = (config.Value == "1");
                }
                else
                {
                    SysConfig defaultConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_IsHault);
                    res = (defaultConfig.Value == "1");
                }

                return res;
            }
        }

        /// <summary>
        /// 汇总单位ID
        /// </summary>
        public string CountUnitID
        {
            get
            {
                string unitID = this.ID;
                if (!this.IsCountUnit)
                {
                    if (this.Parent != null)
                    {
                        unitID = this.Parent.CountUnitID;
                    }
                }
                return unitID;
            }
        }

        /// <summary>
        /// 汇总部门
        /// </summary>
        public Department CountUnit
        {
            get
            {
                // 汇总部门对象延迟加载
                if (this._CountUnit == null && this.CountUnitID != "")
                {
                    this._CountUnit = UserHelper.GetDepartmentByID(this.CountUnitID);
                }
                return this._CountUnit;
            }
        }

        /// <summary>
        /// 所属线条：职能线条（默认值），市场线条，生产线条
        /// </summary>
        public string DeptRole
        {
            get
            {
                string res = string.Empty;
                MemberConfig config = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_DeptRole);
                if (config != null)
                {
                    res = config.Value;
                }
                else
                {
                    SysConfig defaultConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_DeptRole);
                    res = defaultConfig.Value;
                }

                return res;
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 创建部门对象
        /// </summary>
        /// <param name="id">部门唯一编码</param>
        internal Department(string id)
            : base(id)
        {
        }
        /// <summary>
        /// 创建部门对象
        /// </summary>
        /// <param name="id">部门唯一编码</param>
        /// <param name="pID">父部门唯一编码</param>
        internal Department(string id, string pID) : this(id)
        {
            //if (pID == Guid.Empty)
            //{
            //    throw new ArgumentNullException("pID", "创建部门对象时，父部门唯一编码（pID）不能为空（Guid.Empty），若没有父级部门，请使用new Department(Guid id)方法。");
            //}

            this._ParentID = pID;
        }
        #endregion

        #region 私有方法
        private List<User> GetManagers()
        {
            Role role = UserHelper.GetRoleByName("室经理");
            return UserHelper.GetUsersByRole(this, role, true);
        }
        #endregion
    }
}
